package cn.kn.triage.sys.controller;

import cn.kn.triage.common.model.PageBean;
import cn.kn.triage.common.model.R;
import cn.kn.triage.sys.model.User;
import cn.kn.triage.sys.model.UserRole;
import cn.kn.triage.sys.service.UserService;
import cn.kn.triage.sys.vo.IUserVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @author yinzn
 * @create 2020-10-19 14:31
 */
@RestController
@RequestMapping("sys/user")
@CrossOrigin
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping
    public R add(@RequestBody User user) {
        return userService.add(user);
    }

    @DeleteMapping("/{userId}")
    public R del(@PathVariable Integer userId) {
        return userService.del(userId);
    }

    @PutMapping("/{userId}")
    public R update(@PathVariable Integer userId, @RequestBody IUserVo iUserVo){
        return userService.update(userId,iUserVo);
    }

    @RequiresPermissions("sys:role:page")
    @GetMapping
    public R get(){
        return userService.get();
    }

    @GetMapping("list")
    public R list(PageBean pageBean,String userName){
        return userService.list(pageBean,userName);
    }

    @PostMapping("saveRoles/{userId}/{roles}")
    public R saveRoles(@PathVariable Integer userId, @PathVariable String roles, @RequestBody UserRole [] userRoles){
       return userService.saveRoles(userId,roles,userRoles);
    }

    @GetMapping("/{userId}")
    public R getRoleByUserId(@PathVariable Integer userId){
        return userService.getRoleByUserId(userId);
    }

    @GetMapping("menu")
    public R getMenu(){
        return userService.getMenu();
    }

    @GetMapping("login")
    public R login(@NotBlank(message = "账号必填") String username,
                   @NotBlank(message = "密码必填") String password){
return userService.login(username,password);
    }
}
